# -*- coding:utf-8 -*-
# @Time : 2020/11/23 17:49
# @Author: limeng
# @File : user_service.py
import hashlib

from application import db
from application.libs import const
from application.models.mantis_models import User
from application.models.models import MantisUserTable


class UserService:

    @classmethod
    def login(cls, u, p):
        """
        登录验证用户名密码
        :param u: 用户名
        :param p: 密码
        :return:
        """
        try:
            p_md5 = hashlib.md5(p.encode(encoding='UTF-8')).hexdigest()
            userinfo = db.session.query(MantisUserTable).filter(MantisUserTable.username == u).one()
            if userinfo:
                if userinfo.password == p_md5:
                    token = u
                    return True, {const.TOKEN: token}
                else:
                    # token = "密码错误"
                    return False, "密码错误"
        except Exception as e:
            data = "用户不存在"
        return False, data


    @classmethod
    def user_info(cls, username):
        """
        用户信息
        :param username: 用户名
        :return:string
        """
        try:
            user_info_o = db.session.query(MantisUserTable.id.label("uid"), MantisUserTable.email.label("email"),
                                           MantisUserTable.access_level.label("access_level"),
                                           MantisUserTable.realname.label("realname")).filter(
                MantisUserTable.username == username).one()
        except Exception as e:
            # db.session.rollback()
            print('user_info异常:',e)
            import time
            time.sleep(3)
            user_info_o = db.session.query(MantisUserTable.id.label("uid"), MantisUserTable.email.label("email"),
                                           MantisUserTable.access_level.label("access_level"),
                                           MantisUserTable.realname.label("realname")).filter(
                MantisUserTable.username == username).one()
            
        email = user_info_o.email
        access_level = user_info_o.access_level
        realname = user_info_o.realname
        uid = user_info_o.uid
        admin = [5, 8, 159]
        if uid in admin: access_level = 80
        if access_level >= 70:
            data = {
                "uid": uid,
                "email": email,
                "access_level": access_level,
                "name": realname,
                "roles": ["admin"],
                "introduction": "I am a super administrator",
                "avatar": cls.user_img(uid)
            }
        else:
            data = {
                "uid": uid,
                "email": email,
                "access_level": access_level,
                "name": realname,
                "roles": ["editor"],
                "introduction": "I am a editor",
                "avatar": cls.user_img(uid)
            }
        return data

    @classmethod
    def visitor_info(cls):
        """
        用户信息
        :param username: 用户名
        :return:string
        """
        data = {
            "uid": '10000',
            "email": "test@qq.com",
            "access_level": "test",
            "name": "test",
            "roles": ["editor"],
            "introduction": "I am a super editor",
            "avatar": cls.user_img(85)
        }
        return data

    @staticmethod
    def user_img(uid):
        """
        获取用户图片
        :param uid: 用户id
        :return: string
        """
        try:
            u_img = db.session.query(User).filter(User.uid == uid).one().img
        except:
            u_img = const.DEFAULT_USER_AVATAR
        return u_img

    @classmethod
    def handler_list(cls):
        # m_User = db.session.query(MantisUserTable).filter(
        #     or_(MantisUserTable.realname.like('D-%'), MantisUserTable.realname.like('FE%'))).order_by(MantisUserTable
        #                                                                                               .realname).all()
        m_User = db.session.query(MantisUserTable).filter(
            MantisUserTable.access_level == 40, MantisUserTable.enabled == 1).order_by(MantisUserTable.realname).all()
        data = [{"id": i.id, "realname": i.realname} for i in m_User]
        return data
